package com.maotouying.tmtapp.fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import butterknife.ButterKnife;
import butterknife.Unbinder;

import static android.content.Context.MODE_PRIVATE;


public abstract class BaseFragment extends Fragment {

    protected View mRootView;

    // 绑定事件的属性
    private Unbinder unbinder;


    // 生命周期函数1
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (mRootView == null) {
            // 从子类中传过来的布局，进行初始化
            mRootView = inflater.inflate(initLayout(), container, false);
            initView();
        }

        // 绑定到unbinder
        unbinder = ButterKnife.bind(this, mRootView);

        // mRootView 如果为空，就创建一个mRootView，如果不是null则直接返回。全部由子类来控制
        return mRootView;
    }

    // 生命周期函数2
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

        super.onViewCreated(view, savedInstanceState);
        // 初始化数据
        initDate();
    }

    // 解除绑定
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    // 初始化布局
    protected abstract int initLayout();

    // 初始化控件
    protected abstract void initView();

    // 初始化数据，ps：监听事件
    protected abstract void initDate();

    // toast提示工具类
    public void showToast(String msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }

    // toast提示工具类  异步
    public void showToastSync(String msg) {
        Looper.prepare();
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
        Looper.loop();
    }

    // 跳转到指定Activity，不带参数
    public void navigateTo(Class clazz) {
        Intent intent = new Intent(getActivity(), clazz);
        startActivity(intent);
    }

    // 跳转到指定Activity，带参数
    public void navigateToWithBundle(Class clazz,Bundle bundle) {
        Intent intent = new Intent(getActivity(), clazz);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    // 跳转到指定Activity，带参数
    public void navigateToWithFlag(Class clazz,int flag) {
        Intent intent = new Intent(getActivity(), clazz);
        intent.setFlags(flag);
        startActivity(intent);
    }

    /**
     * 根据key和val，把对应的值以xml形式存到本地
     *
     * @param key
     * @param val
     */
    protected void saveStringToSp(String key, String val) {
        SharedPreferences sp = getActivity().getSharedPreferences("sp_ttit", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, val);
        editor.commit();
    }

    protected String getStringFromSp(String key) {
        SharedPreferences sp = getActivity().getSharedPreferences("sp_ttit", MODE_PRIVATE);
        return sp.getString(key, "");
    }
}